function [] = feature_extraction( moving_average_filtered_data_x, moving_average_filtered_data_y, moving_average_filtered_data_z, max_derivative_point, segment_point1, segment_point2, target, feature_file_name )

% the length of the basic gesture
len = segment_point2 - segment_point1 + 1;

% the average energy on x-axis of the basic gesture 
avg_energy_x = 0; 
% the average energy on y-axis of the basic gesture 
avg_energy_y = 0;

% the average abs on x-axis of the basic gesture
avg_abs_x = 0;
% the average abs on y-axis of the basic gesture
avg_abs_y = 0;

% the mean value on x-axis
mean_x = 0;
% the mean value on y-axis
mean_y = 0;

amplitude_threshold = 0;

for i = segment_point1 : segment_point2
    avg_energy_x = avg_energy_x + moving_average_filtered_data_x(i)^2;
    avg_energy_y = avg_energy_y + moving_average_filtered_data_y(i)^2;
    avg_abs_x = avg_abs_x + abs(moving_average_filtered_data_x(i));
    avg_abs_y = avg_abs_y + abs(moving_average_filtered_data_y(i));
    mean_x = mean_x + moving_average_filtered_data_x(i);
    mean_y = mean_y + moving_average_filtered_data_y(i);
    amplitude_threshold = amplitude_threshold + sqrt(moving_average_filtered_data_x(i)^2 + moving_average_filtered_data_y(i)^2);
end
avg_energy_x = avg_energy_x / len;
avg_energy_y = avg_energy_y / len;
avg_abs_x = avg_abs_x / len;
avg_abs_y = avg_abs_y / len;
mean_x = mean_x / len;
mean_y = mean_y / len;
amplitude_threshold = 0.7 * amplitude_threshold / len;

% the ratio between avg_energy_x and avg_energy_y
ratio_avg_energy_x_y = avg_energy_x / avg_energy_y - 1;
% the ratio between avg_abs_x and avg_abs_y
ratio_avg_abs_x_y = avg_abs_x / avg_abs_y - 1;
% the sign of the ratio_avg_energy_x_y
sign_ratio_avg_energy_x_y = sign(ratio_avg_energy_x_y);
% the sign of the ratio_avg_abs_x_y
sign_ratio_avg_abs_x_y = sign(ratio_avg_abs_x_y);
% the abs of the ratio_avg_energy_x_y
abs_ratio_avg_energy_x_y = abs(ratio_avg_energy_x_y);
% the abs of the ratio_avg_abs_x_y
abs_ratio_avg_abs_x_y = abs(ratio_avg_abs_x_y);

% the sum of avg_energy_x and avg_energy_y
sum_avg_energy_x_y = avg_energy_x + avg_energy_y;
% the sum of avg_abs_x and avg_abs_y
sum_avg_abs_x_y = avg_abs_x + avg_abs_y;

% the difference value between avg_energy_x and avg_energy_y
delta_avg_energy_x_y = avg_energy_x - avg_energy_y;
% the difference value between avg_abs_x and avg_abs_y
delta_avg_abs_x_y = avg_abs_x - avg_abs_y;
% the sign of the difference value delta_avg_energy_x_y
sign_delta_avg_energy_x_y = sign(delta_avg_energy_x_y);
% the sign of the difference value delta_avg_abs_x_y
sign_delta_avg_abs_x_y = sign(delta_avg_abs_x_y);
% the abs of the difference value delta_avg_energy_x_y
abs_delta_avg_energy_x_y = abs(delta_avg_energy_x_y);
% the abs of the difference value delta_avg_abs_x_y
abs_delta_avg_abs_x_y = abs(delta_avg_abs_x_y);


% the sign of the absolute max point on the left hand side of the max_derivative_point (x-axis)
sign_lhs_x = 0;
% the sign of the absolute max point on the left hand side of the max_derivative_point (y-axis)
sign_lhs_y = 0;
% the sign of the absolute max point on the right hand side of the max_derivative_point (x-axis)
sign_rhs_x = 0;
% the sign of the absolute max point on the right hand side of the max_derivative_point (y-axis)
sign_rhs_y = 0;

% the sign of the absolute max point larger than threshold on the left hand side of the max_derivative_point (x-axis)
sign_threshlod_lhs_x = 0;
% the sign of the absolute max point larger than threshold on the left hand side of the max_derivative_point (y-axis)
sign_threshlod_lhs_y = 0;
% the sign of the absolute max point larger than threshold on the right hand side of the max_derivative_point (x-axis)
sign_threshlod_rhs_x = 0;
% the sign of the absolute max point larger than threshold on the right hand side of the max_derivative_point (y-axis)
sign_threshlod_rhs_y = 0;

% the points of the absolute max point larger than threshold on the left hand side of the max_derivative_point (x-axis)
point_count_threshold_lhs_x = 0;
% the points of the absolute max point larger than threshold on the left hand side of the max_derivative_point (y-axis)
point_count_threshold_lhs_y = 0;
% the points of the absolute max point larger than threshold on the right hand side of the max_derivative_point (x-axis)
point_count_threshold_rhs_x = 0;
% the points of the absolute max point larger than threshold on the right hand side of the max_derivative_point (y-axis)
point_count_threshold_rhs_y = 0;

% the absolute max value on the left hand side of the max_derivative_point (x-axis)
max_lhs_abs_x = 0;
% the absolute max value on the left hand side of the max_derivative_point (y-axis)
max_lhs_abs_y = 0;
% the absolute max value on the right hand side of the max_derivative_point (x-axis)
max_rhs_abs_x = 0;
% the absolute max value on the right hand side of the max_derivative_point (y-axis)
max_rhs_abs_y = 0;

% the value of absolute max on the left hand side of the max_derivative_point (x-axis)
max_lhs_x = 0;
% the value of absolute max on the left hand side of the max_derivative_point (y-axis)
max_lhs_y = 0;
% the value of absolute max on the right hand side of the max_derivative_point (x-axis)
max_rhs_x = 0;
% the value of absolute max on the right hand side of the max_derivative_point (y-axis)
max_rhs_y = 0;

max_lhs_x_t = 0;
max_lhs_y_t = 0;
max_rhs_x_t = 0;
max_rhs_y_t = 0;
for i = segment_point1 : max_derivative_point - 1
    abs_x = abs(moving_average_filtered_data_x(i));
    abs_y = abs(moving_average_filtered_data_y(i));
    if max_lhs_abs_x < abs_x
        max_lhs_abs_x = abs_x;
        max_lhs_x_t = i;
        max_lhs_x = moving_average_filtered_data_x(i);
        sign_lhs_x = sign(moving_average_filtered_data_x(i));
    end
    if max_lhs_abs_y < abs_y
        max_lhs_abs_y = abs_y;
        max_lhs_y_t = i;
        max_lhs_y = moving_average_filtered_data_y(i);
        sign_lhs_y = sign(moving_average_filtered_data_y(i));
    end
    if abs_x > amplitude_threshold
        point_count_threshold_lhs_x = point_count_threshold_lhs_x + 1;
    end
    if abs_y > amplitude_threshold
        point_count_threshold_lhs_y = point_count_threshold_lhs_y + 1;
    end
end

for i = max_derivative_point : segment_point2
    abs_x = abs(moving_average_filtered_data_x(i));
    abs_y = abs(moving_average_filtered_data_y(i));
    if max_rhs_abs_x < abs_x
        max_rhs_abs_x = abs_x;
        max_rhs_x_t = i;
        max_rhs_x = moving_average_filtered_data_x(i);
        sign_rhs_x = sign(moving_average_filtered_data_x(i));
    end
    if max_rhs_abs_y < abs_y
        max_rhs_abs_y = abs_y;
        max_rhs_y_t = i;
        max_rhs_y = moving_average_filtered_data_y(i);
        sign_rhs_y = sign(moving_average_filtered_data_y(i));
    end
    if abs_x > amplitude_threshold
        point_count_threshold_rhs_x = point_count_threshold_rhs_x + 1;
    end
    if abs_y > amplitude_threshold
        point_count_threshold_rhs_y = point_count_threshold_rhs_y + 1;
    end
end

if max_lhs_abs_x > amplitude_threshold
    sign_threshlod_lhs_x = sign(moving_average_filtered_data_x(max_lhs_x_t));
end
if max_lhs_abs_y > amplitude_threshold
    sign_threshlod_lhs_y = sign(moving_average_filtered_data_y(max_lhs_y_t));
end
if max_rhs_abs_x > amplitude_threshold
    sign_threshlod_rhs_x = sign(moving_average_filtered_data_x(max_rhs_x_t));
end
if max_rhs_abs_y > amplitude_threshold
    sign_threshlod_rhs_y = sign(moving_average_filtered_data_y(max_rhs_y_t));
end

% the average jerk between the two max points on x-axis
j_x = (max_rhs_x - max_lhs_x) / (max_rhs_x_t - max_lhs_x_t);
% the average jerk between the two max points on y-axis
j_y = (max_rhs_y - max_lhs_y) / (max_rhs_y_t - max_lhs_y_t);
% the sign of j_x
sign_j_x = sign(j_x);
% the sign of j_y
sign_j_y = sign(j_y);
% the abs of j_x
abs_j_x = abs(j_x);
% the abs of j_y
abs_j_y = abs(j_y);
% the ratio between j_x and j_y
ratio_j_x_y = j_x / j_y;
% the sign of ratio_j_x_y
sign_ratio_j_x_y = sign(ratio_j_x_y);
% the abs of ratio_j_x_y
abs_ratio_j_x_y = abs(ratio_j_x_y);

% the difference value between max_lhs_x and max_lhs_y
delta_max_lhs_x_y = max_lhs_x - max_lhs_y;
% the difference value between max_rhs_x and max_rhs_y
delta_max_rhs_x_y = max_rhs_x - max_rhs_y;
% the sign of delta_max_lhs_x_y
sign_delta_max_lhs_x_y = sign(delta_max_lhs_x_y);
% the sign of delta_max_rhs_x_y
sign_delta_max_rhs_x_y = sign(delta_max_rhs_x_y);
% the abs of delta_max_lhs_x_y
abs_delta_max_lhs_x_y = abs(delta_max_lhs_x_y);
% the abs of delta_max_rhs_x_y
abs_delta_max_rhs_x_y = abs(delta_max_rhs_x_y);


% the variance on x-axis
var_x = 0;
% the variance on y-axis
var_y = 0;

% the mean absolute derivation on x-axis
mad_x = 0;
% the mean absolute derivation on y-axis
mad_y = 0;

for i = segment_point1 : segment_point2
    var_x = var_x + (moving_average_filtered_data_x(i) - mean_x)^2;
    var_y = var_y + (moving_average_filtered_data_y(i) - mean_y)^2;
    mad_x = mad_x + abs(moving_average_filtered_data_x(i) - mean_x);
    mad_y = mad_y + abs(moving_average_filtered_data_y(i) - mean_y);
end
var_x = var_x / (len - 1);
var_y = var_y / (len - 1);
mad_x = mad_x / len;
mad_y = mad_y / len;

% the standard derivation on x-axis
std_x = sqrt(var_x);
% the standard derivation on y-axis
std_y = sqrt(var_y);

% root mean square on x-axis
rms_x = sqrt(avg_energy_x);
% root mean square on y-axis
rms_y = sqrt(avg_energy_y);

cov_x_y = 0;
for i = segment_point1 : segment_point2
    cov_x_y = cov_x_y + (moving_average_filtered_data_x(i) - mean_x) * (moving_average_filtered_data_y(i) - mean_y);
end
cov_x_y = cov_x_y / (len - 1);

% correlation coefficient
correlation_coefficient = cov_x_y / (std_x * std_y);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% write the features to a file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen(feature_file_name, 'a+');

% fprintf(fid, '%d,', len);
% fprintf(fid, '%.2f,', avg_energy_x);
% fprintf(fid, '%.2f,', avg_energy_y);
fprintf(fid, '%.2f,', avg_abs_x);
fprintf(fid, '%.2f,', avg_abs_y);
% fprintf(fid, '%.2f,', mean_x);
% fprintf(fid, '%.2f,', mean_y);
% fprintf(fid, '%.2f,', ratio_avg_energy_x_y);
fprintf(fid, '%.2f,', ratio_avg_abs_x_y);
% fprintf(fid, '%d,', sign_ratio_avg_energy_x_y);
% fprintf(fid, '%d,', sign_ratio_avg_abs_x_y);
% fprintf(fid, '%.2f,', abs_ratio_avg_energy_x_y);
% fprintf(fid, '%.2f,', abs_ratio_avg_abs_x_y);
% fprintf(fid, '%.2f,', sum_avg_energy_x_y);
% fprintf(fid, '%.2f,', sum_avg_abs_x_y);
% fprintf(fid, '%.2f,', delta_avg_energy_x_y);
% fprintf(fid, '%.2f,', delta_avg_abs_x_y);
% fprintf(fid, '%d,', sign_delta_avg_energy_x_y);
% fprintf(fid, '%d,', sign_delta_avg_abs_x_y);
% fprintf(fid, '%.2f,', abs_delta_avg_energy_x_y);
fprintf(fid, '%.2f,', abs_delta_avg_abs_x_y);
% fprintf(fid, '%d,', sign_lhs_x);
% fprintf(fid, '%d,', sign_lhs_y);
% fprintf(fid, '%d,', sign_rhs_x);
% fprintf(fid, '%d,', sign_rhs_y);
fprintf(fid, '%d,', sign_threshlod_lhs_x);
fprintf(fid, '%d,', sign_threshlod_lhs_y);
fprintf(fid, '%d,', sign_threshlod_rhs_x);
fprintf(fid, '%d,', sign_threshlod_rhs_y);
% fprintf(fid, '%d,', point_count_threshold_lhs_x);
% fprintf(fid, '%d,', point_count_threshold_lhs_y);
% fprintf(fid, '%d,', point_count_threshold_rhs_x);
% fprintf(fid, '%d,', point_count_threshold_rhs_y);
% fprintf(fid, '%.2f,', max_lhs_abs_x);
% fprintf(fid, '%.2f,', max_lhs_abs_y);
% fprintf(fid, '%.2f,', max_rhs_abs_x);
% fprintf(fid, '%.2f,', max_rhs_abs_y);
fprintf(fid, '%.2f,', max_lhs_x);
fprintf(fid, '%.2f,', max_lhs_y);
fprintf(fid, '%.2f,', max_rhs_x);
fprintf(fid, '%.2f,', max_rhs_y);
fprintf(fid, '%.2f,', j_x);
fprintf(fid, '%.2f,', j_y);
fprintf(fid, '%d,', sign_j_x);
fprintf(fid, '%d,', sign_j_y);
% fprintf(fid, '%.2f,', abs_j_x);
% fprintf(fid, '%.2f,', abs_j_y);
fprintf(fid, '%.2f,', ratio_j_x_y);
fprintf(fid, '%d,', sign_ratio_j_x_y);
% fprintf(fid, '%.2f,', abs_ratio_j_x_y);
fprintf(fid, '%.2f,', delta_max_lhs_x_y);
fprintf(fid, '%.2f,', delta_max_rhs_x_y);
fprintf(fid, '%d,', sign_delta_max_lhs_x_y);
fprintf(fid, '%d,', sign_delta_max_rhs_x_y);
fprintf(fid, '%.2f,', abs_delta_max_lhs_x_y);
fprintf(fid, '%.2f,', abs_delta_max_rhs_x_y);
% fprintf(fid, '%.2f,', var_x);
% fprintf(fid, '%.2f,', var_y);
% fprintf(fid, '%.2f,', mad_x);
% fprintf(fid, '%.2f,', mad_y);
% fprintf(fid, '%.2f,', std_x);
% fprintf(fid, '%.2f,', std_y);
% fprintf(fid, '%.2f,', rms_x);
% fprintf(fid, '%.2f,', rms_y);
fprintf(fid, '%.2f,', correlation_coefficient);
fprintf(fid, '%s\n', target);

fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end